package com.class05.listener;

import javax.servlet.*;

public class MyApplicationListener implements ServletContextListener , ServletContextAttributeListener {
//    1、应用域对象创建时调用
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("应用域对象创建了");
    }

//    2、应用域对象销毁时调用
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("应用域对象销毁了");
    }
//    3、添加属性时调用
    @Override
    public void attributeAdded(ServletContextAttributeEvent event) {
//        取出属性名与属性值
        System.out.println("添加了属性名为：" + event.getName());
        System.out.println("添加了属性值为：" + event.getValue());
    }
//    4、删除属性时调用
    @Override
    public void attributeRemoved(ServletContextAttributeEvent event) {
        System.out.println("删除了属性名" + event.getName());
        System.out.println("删除了属性值" + event.getValue());
    }
//    5、替换属性时调用
    public void attributeReplaced(ServletContextAttributeEvent event) {
        System.out.println("替换了属性名为：" + event.getName());

        ServletContext servletContext = event.getServletContext();
//        获取应用上下文路径
        String contextPath = servletContext.getContextPath();
        System.out.println("上下文路径为：" + contextPath);
        ClassLoader classLoader = servletContext.getClassLoader();
        System.out.println("获取到的类加载器为：" + classLoader);
        System.out.println("替换了属性值为：" + event.getValue());
    }
}
